import {
	HANDLE_LOGIN_STATUS,
	HANDLE_LOGIN,
	HANDLE_CHANGE_TABNAME,
	HANDLE_CHANGE_IMPORT_TYPE,
	HANDLE_SET_PAYROLL_INFO
} from './../type'
import { userLogin, userLogout, postPayrollSetInfo } from '@/APIs/index/index'

const actions = {
	async handleLoginStatus({ dispatch, commit }, { code, state }) {
		await dispatch('handleLogin', { code, state })
		commit(HANDLE_LOGIN_STATUS, true)
	},
	async handleLogin({ commit }, { code, state }) {
		const { data } = await userLogin({ code, state })
		commit(HANDLE_LOGIN, { ...data })
		return data
	},
	async _handleLogout({ commit }) {
		await userLogout()
		commit(HANDLE_LOGIN, {})
		commit(HANDLE_LOGIN_STATUS, false)
	},
	handleLogout({ commit }) {
		commit(HANDLE_LOGIN, {})
		commit(HANDLE_LOGIN_STATUS, false)
	},
	handleChangeTabName({ commit }, tabNames) {
		commit(HANDLE_CHANGE_TABNAME, tabNames)
	},
	handleChangeImportType({ commit }, importType) {
		commit(HANDLE_CHANGE_IMPORT_TYPE, importType)
	},
	async handleSetPayrollInfo({ commit }) {
		const { data } = await postPayrollSetInfo()
		commit(HANDLE_SET_PAYROLL_INFO, { ...data })
		return data
	}
}

export default actions
